package segmenter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import edu.stanford.nlp.ie.crf.CRFClassifier;
import edu.stanford.nlp.ling.CoreLabel;

public class ChineseSegment {
	
	private static final String basedir = System.getProperty("SegDemo", "data");
	private volatile static ChineseSegment instance;
	private volatile static CRFClassifier<CoreLabel> segmenter;
	private static Set<String> stopWord ;
	static{
		stopWord = readStopword("models/chineseStopword.txt");
	}
	public static ChineseSegment getInstance(){
		if(instance == null ){
			synchronized (ChineseSegment.class) {
				if(instance == null){
					Properties props = new Properties();
					props.setProperty("sighanCorporaDict", basedir);
					props.setProperty("serDictionary", basedir + "/dict-chris6.ser.gz");
					props.setProperty("inputEncoding", "UTF-8");
					props.setProperty("sighanPostProcessing", "true");
					segmenter = new CRFClassifier<CoreLabel>(props);
					segmenter.loadClassifierNoExceptions(basedir + "/ctb.gz", props);
					instance = new ChineseSegment ();
				}
			}
		}
		return instance;
	}
	
	
	public String segmentSen(String source){
		if(source == null)
			return null;
		List<String> segmented = segmenter.segmentString(source);
		StringBuffer sBuffer = new StringBuffer();
		for(String seg : segmented){
			if(!stopWord.contains(seg))
				sBuffer.append(seg).append(" ");
		}
		String result = sBuffer.toString().trim();
		return result;
	}
	
	public static Set <String> readStopword(String path) {
		HashSet< String> set = new HashSet<String>();
		try {
			BufferedReader bReader = new BufferedReader(new FileReader(new File(path)));
			String  word = "";
				while((word=bReader.readLine())!=null){
					set.add(word);
				}
			}catch (Exception e) {
				e.printStackTrace();
			}		
		return set;	
	}
}
